package view;

import model.Constants;
import model.ImageSelector;

import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Layout;
import org.eclipse.swt.widgets.Shell;

import control.Util;

public class MemoryFrame {

	private Display display;
	private Shell shell;

	public MemoryFrame() {

		display = Display.getCurrent();
		shell = new Shell(display);
		configureShell();
		keepAlive();
	}

	private void buildContent() {

		ImageSelector.getInstance().buildImages();

		for (int i = 0; i <= 35; i++) {
			new MemoryCard(shell, i);

		}

	}

	private void configureShell() {
		shell.setLayout(getShellLayout());
		shell.setText(Constants.MEMORYTITLE);
		buildContent();
		shell.pack();
		Util.centerFrame(shell);
		shell.setVisible(true);
		shell.forceFocus();
	}

	private Layout getShellLayout() {
		GridLayout layout = new GridLayout(6, false);
		layout.marginLeft = 10;
		layout.marginRight = 10;
		layout.marginTop = 10;
		layout.marginBottom = 10;
		return layout;
	}

	private void keepAlive() {

		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}

	}

}
